package cannot.backcontroller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cannot.pojo.MainType;
import cannot.pojo.ShowIndex;
import cannot.pojo.ShowIndexImage;
import cannot.pojo.Tv;
import cannot.service.MainTypeService;
import cannot.service.ShowIndexImageService;
import cannot.service.ShowIndexService;
import cannot.service.TvService;
import cannot.util.FileUtil;
import cannot.util.ImageUtil;
import cannot.util.StoragePlace;
/**
 * 管理员，首页管理控制器
 * @author Administrator
 *
 */
@Controller
public class IndexController {
	
	String imgPath =StoragePlace.basicPlace+"img"+File.separator+"big"+File.separator;
	String imgPathSmall =StoragePlace.basicPlace+"img"+File.separator+"small"+File.separator;

	@Autowired
	MainTypeService mainTypeService;
	@Autowired
	ShowIndexService showIndexService;
	@Autowired
	ShowIndexImageService showIndexImageService;
	@Autowired
	TvService tvService;
	/**
	 * 首页管理→首页排版管理
	 * @param model
	 * @return
	 */
	@RequestMapping("admin_rightContent_subIndex")
	public String adminRightContentSubIndex(Model model){
		List<MainType> mainTypes = mainTypeService.listMainType();
		model.addAttribute("mainTypes", mainTypes);
		 return "admin/rightContent/subIndex";
	}
	/**
	 * 首页管理→首页轮播图管理
	 * @param model
	 * @return
	 */
	@RequestMapping("admin_rightContent_subIndexImage")
	public String adminRightContentSubIndexImage(Model model){
		List<ShowIndexImage> showIndexImages = showIndexImageService.showIndexImages();
		List<Tv> tvs = tvService.listTvs();
		for(ShowIndexImage showIndexImage:showIndexImages){
			Tv tv = tvService.selectById(showIndexImage.getTv_id());
			showIndexImage.setTv(tv);
		}
		model.addAttribute("showIndexImages", showIndexImages);
		model.addAttribute("tvs", tvs);
		 return "admin/rightContent/subIndexImage";
	}
	/**
	 * 首页排版的操作
	 * @param showIndexs
	 * @return
	 */
	@RequestMapping("admin_subIndex_subShowIndex")
	@ResponseBody
	public String adminSubIndexSubShowIndex(@RequestBody List<ShowIndex> showIndexs){
		for(ShowIndex showIndex:showIndexs){
			if(showIndex.getId()!=null&&showIndex.getId()!=0){
				showIndexService.updateShowIndex(showIndex);
			}else{
				showIndexService.insertShowIndex(showIndex);
			}
		}
		return "SUCCESS";
	}
	
	/**
	 * 传输大图图片
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/admin_uploadTvBigPicture")
	@ResponseBody
    public String uploadPicture(HttpServletRequest req,@RequestPart("file") MultipartFile picture) {
    	  String pictureName = UUID.randomUUID().toString() + ".png";
          try {
              File file = new File(imgPath);
              file.mkdirs();
              File f = new File(imgPath + pictureName);
              picture.transferTo(f);
              ImageUtil.resizeImage(f,1380, 480, f);
          } catch (Exception e) {
          }
          return pictureName;
    }
	/**
	 * 获取大图图片
	 */
	@RequestMapping("virtual_imgBig")
    public void renderPicture(String pictureId, HttpServletResponse response) {
        String path =imgPath+ pictureId;
        try {
            byte[] bytes = FileUtil.toByteArray(path);
            response.getOutputStream().write(bytes);
        }catch (Exception e){
            //如果找不到图片就返回一个默认图片
            try {
                response.sendRedirect("img/adminUploadIndex.png");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
	
	/**
	 * 传输小图
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/admin_uploadTvSmallPicture")
	@ResponseBody
    public String uploadSmallPicture(HttpServletRequest req,@RequestPart("file") MultipartFile picture) {
    	  String pictureName = UUID.randomUUID().toString() + ".jpg";
          try {
              File file = new File(imgPathSmall);
              file.mkdirs();
              File f = new File(imgPathSmall + pictureName);
              picture.transferTo(f);
              ImageUtil.resizeImage(f,135, 75, f);
          } catch (Exception e) {
          }
          return pictureName;
    }
	/**
	 * 获取小图
	 */
	@RequestMapping("virtual_imgSmall")
    public void renderPictureSmall(String pictureId, HttpServletResponse response) {
        String path =imgPathSmall+ pictureId;
        try {
            byte[] bytes = FileUtil.toByteArray(path);
            response.getOutputStream().write(bytes);
        }catch (Exception e){
            //如果找不到图片就返回一个默认图片
            try {
                response.sendRedirect("img/adminUploadIndex.png");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
	/*
	 * 更新轮播图、大图的信息
	 */
	@RequestMapping("admin_subIndexImage_subShowIndexImage")
	@ResponseBody
	public Object subShowIndexImage(@RequestBody List<ShowIndexImage> showIndexImages){
		for(ShowIndexImage showIndexImage:showIndexImages){
			showIndexImageService.updateIndexImage(showIndexImage);
		}
		return "SUCCESS";
	}
}
